import sexPieChart from './sex-pie-chart.vue'
import userSourceChart from './user-source-chart.vue'
import chargeTimeChart from './charge-time-chart.vue'
import latelyChargeTimeChart from './lately-charge-time-chart.vue'
import rechargeAmountChart from './recharge-amount-chart.vue'
import latelyRechargeAmountChart from './lately-recharge-amount-chart.vue'
import latelyRechargeCountChart from './lately-recharge-count-chart.vue'
import latelyChargeCountChart from './lately-charge-count-chart.vue'

import UserDistributeChart from './user-distribute-chart.vue'

import { Request } from '@/utils'

export default {
    data () {
        return {
            sexCategoryData : null, //性别
            userSourceData : null, //用户来源
            chargeTotalTimeData : null, //充电累积时间
            latelyChargeTimeData : null, //近七日充电时间
            rechargeTotalAmountData : null, //充值累积金额
            latelyRechargeAmountData : null, //近七日充值金额

            latelyRechargeCountData : null, //近七日充值次数
            latelyChargeCountData : null, //近七日充电次数
            
            userDistributeChartData : null, //用户地域分布数据
        }
    },

    components : {
        sexPieChart,
        userSourceChart,
        chargeTimeChart,
        latelyChargeTimeChart,
        rechargeAmountChart,
        latelyRechargeAmountChart,

        latelyRechargeCountChart,
        latelyChargeCountChart,
        UserDistributeChart
    },

    methods : {
        run () {
            (new Request()).get('statistic/user/statisticUserGender', res => { //获取用户性别
                var sexCategoryData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(res => {
                        var data = {
                            gender : res.gender,
                            value : res.genderNum
                        }
                        switch(res.gender) {
                            case 1 : data.name = '男'
                                break
                            case 2 : data.name = '女'
                                break
                            default : data.name = '未知'
                        }

                        sexCategoryData.push(data)
                    })
                }

                this.sexCategoryData = sexCategoryData
            }); 

            (new Request()).get('statistic/user/statisticUserType', res => { //用户来源
                var sourceType = {1 : '微信', 2 : '平台', 3 : '支付宝', 4 : '小程序'}
                var userSourceData = []
                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(res => {
                        var data = {
                            name : sourceType[res.type],
                            value : res.typeNum
                        }
                        userSourceData.push(data)
                    })
                }

                this.userSourceData = userSourceData
            });

            (new Request()).get('statistic/user/getStatisticChargeTime', res => { //充电累积时间
                var chargeTotalTimeData = []
                if(res.success && res.data) {
                    var data = res.data
                    chargeTotalTimeData.push({name : '电动车', value : data.electrocar || 0})
                    chargeTotalTimeData.push({name : '汽车', value : data.car || 0})
                    chargeTotalTimeData.push({name : '充电柜', value : data.chargeCabinet || 0})
                    chargeTotalTimeData.push({name : '换电柜', value : data.exchangeCabinet || 0})
                }

                this.chargeTotalTimeData = chargeTotalTimeData
            });

            (new Request()).get('statistic/user/totalSevenDaysChargeTime', res => { //获取近七天充电时间
                var latelyChargeTimeData = {
                    date : [],
                    data : {
                        total : [],
                        avg : []
                    }
                }

                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        latelyChargeTimeData.date.push(item.date)
                        latelyChargeTimeData.data.total.push(((item.consumeQuantity || 0) / 60).toFixed(2))
                        latelyChargeTimeData.data.avg.push(((item.avgConsumeQuantity || 0) / 60).toFixed(2))
                    })
                }

                this.latelyChargeTimeData = latelyChargeTimeData
    
            });


            (new Request()).get('statistic/user/getStatisticRechargeAmount', res => { //充值累积金额
                var chargeTotalTimeData = []
                if(res.success && res.data) {
                    var data = res.data
                    chargeTotalTimeData.push({name : '微信', value : data.wechat || 0})
                    chargeTotalTimeData.push({name : '支付宝', value : data.alipay || 0})
                    chargeTotalTimeData.push({name : '平台', value : data.platform || 0})
                }

                this.rechargeTotalAmountData = chargeTotalTimeData
            });

            (new Request()).get('statistic/user/totalSevenDaysRechargeAmount', res => { //获取近七天充值金额
                var latelyRechargeAmountData = {
                    date : [],
                    data : {
                        total : [],
                        avg : []
                    }
                }

                if(res.success && res.data && res.data.length > 0) {
                    res.data.forEach(item => {
                        latelyRechargeAmountData.date.push(item.date)
                        latelyRechargeAmountData.data.total.push(item.amount || 0)
                        latelyRechargeAmountData.data.avg.push(item.avgAmount || 0)
                    })
                }

                this.latelyRechargeAmountData = latelyRechargeAmountData
    
            });

            (new Request()).get('statistic/user/totalSevenDaysRechargeNum', res => { //获取近七天充值次数
                var latelyRechargeCountData = {
                    date : [],
                    wechat : [],
                    alipay : [],
                    platform : []
                }
                if(res.success && res.data) {
                    res.data.forEach(item => {
                        latelyRechargeCountData.date.push(item.date)
                        latelyRechargeCountData.wechat.push(item.wechat || 0)
                        latelyRechargeCountData.alipay.push(item.alipay ||  0)
                        latelyRechargeCountData.platform.push(item.platform ||  0)
                    })
                }

                this.latelyRechargeCountData = latelyRechargeCountData
            });

            (new Request()).get('statistic/user/totalSevenDaysChargeNum', res => { //获取近七天充电次数
                var latelyChargeCountData = {
                    date : [],
                    data : [
                        [], //电单车
                        [], //汽车
                        [], //换电柜
                        [], //充电柜
                    ]
                }
                
                if(res.success && res.data) {
                    res.data.forEach(item => {
                        latelyChargeCountData.date.push(item.date)

                        latelyChargeCountData.data[0].push(item.electrocar || 0)
                        latelyChargeCountData.data[1].push(item.car || 0)
                        latelyChargeCountData.data[2].push(item.exchangeCabinet || 0)
                        latelyChargeCountData.data[3].push(item.chargeCabinet || 0)
                    })
                }
                this.latelyChargeCountData = latelyChargeCountData

            });

            (new Request()).get('statistic/user/statisticUserArea', res => { //获取用户地域分布
                var userDistributeChartData = {
                    area : [],
                    data : []
                }
                if(res.success && res.data) {
                    res.data.forEach(item => {
                        userDistributeChartData.area.push(item.areaName)
                        userDistributeChartData.data.push(item.totalNum || 0)
                    })
                }
                this.userDistributeChartData = userDistributeChartData
            });

            
            setTimeout(() => {
                this.run()
            }, 30 * 60 * 1000)

        }
    },

    mounted () {
        this.run()
    }
}